/*
 * @Description: 新增元数据弹窗组件
 * @Author: Rfan
 * @Date: 2022-05-28 12:10:37
 * @LastEditTime: 2022-06-20 17:31:14
 */

import { useEffect } from 'react';
import { Button, Form, Modal } from 'antd';
import { useBoxMgtContext } from '../../contexts/BoxMgtContext';
import BoxForm from './common/BoxForm';
import type { FC } from 'react';
import type { ICommonDialog } from '@/interfaces/global';

interface IEditDictionaryDialogProps extends ICommonDialog {
  data: any;
}

const EditDictionaryDialog: FC<IEditDictionaryDialogProps> = (props) => {
  const { visible, data, onClose } = props;
  const { editBoxFunc } = useBoxMgtContext();
  const [form] = Form.useForm();

  useEffect(() => {
    if (visible) {
      form.setFieldsValue(data);
    }
  }, [visible]);

  // 提交表单
  const submitFunc = () => {
    form.submit();
  };

  // 新增档案类型
  const onFinish = (values: any) => {
    editBoxFunc({ ...data, ...values }).then(() => {
      onClose();
    });
  };

  const onCancel = () => {
    onClose();
  };

  return (
    <Modal
      destroyOnClose
      title="编辑盒"
      visible={visible}
      onCancel={onCancel}
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={submitFunc}>
          保存
        </Button>
      ]}
    >
      <Form
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 14 }}
        preserve={false}
        form={form}
        onFinish={onFinish}
      >
        <BoxForm visible={visible} />
      </Form>
    </Modal>
  );
};

export default EditDictionaryDialog;
